﻿using System.Collections.Generic;
using System.Reflection;
using System.Windows.Media;
using Dsl.Ped.Common;
using Dsl.Ped.Common.Helpers;
using Dsl.Ped.Desktop.Helpers;
using Dsl.Ped.Desktop.Windows;
using Dsl.Ped.Common.Extensions;
using Dsl.Ped.Framework.AddIns;

namespace Dsl.Ped.Desktop.ViewModels
{
    /// <summary>
    /// This is the view model of the <see cref="AboutWindow"/>
    /// </summary>
    public class AboutViewModel : ViewModelBase
    {
        #region Fields

        /// <summary>
        /// Holds all the addIns which are injected in DSLPed application
        /// </summary>
        private DSLPedAddIn selectedAddIn;

        #endregion

        #region Subclass

        /// <summary>
        /// Class which is bound into a window and represents a <see cref="IDslPedAddIn"/>
        /// </summary>
        public class DSLPedAddIn
        {
            /// <summary>
            /// Name of the AddIn
            /// </summary>
            public string Name { get; set; }

            /// <summary>
            /// Description of an AddIn
            /// </summary>
            public string Description { get; set; }

            /// <summary>
            /// Icon of an AddIn
            /// </summary>
            public ImageSource Icon { get; set; }

            /// <summary>
            /// Namespace of an addIn
            /// </summary>
            public string Namespace { get; set; }

            /// <summary>
            /// Version of an AddIn
            /// </summary>
            public string Version { get; set; }

            /// <summary>
            /// File extension of the AddIn
            /// </summary>
            public string Extension { get; set; }
        }

        #endregion

        #region Members

        /// <summary>
        /// Gets all AddIns that have been loaded into DSLPed application.
        /// Transform AddIns with additional information into a list of <see cref="DSLPedAddIn"/> object
        /// </summary>
        public IEnumerable<DSLPedAddIn> AddIns
        {
            get
            {
                var addIns = new List<DSLPedAddIn>();
                foreach (var addIn in ApplicationResources.Instance.AvalableAddIns)
                {
                    Assembly applicationAssembly = TypeHelper.GetAssembly(addIn.Metadata.ProjectiveType);

                    addIns.Add(new DSLPedAddIn()
                    {
                        Name = addIn.Metadata.Name,
                        Namespace = addIn.Metadata.Namespace,
                        Extension = addIn.Metadata.Extension,
                        Description = addIn.Metadata.Description,
                        Version = applicationAssembly.GetLongVersion().ToString(),
                        Icon = AddInMetadataHelper.GetAddInImageSource(addIn.Metadata)
                    });
                }

                return addIns;
            }
        }

        /// <summary>
        /// Currently selected AddIn
        /// </summary>
        public DSLPedAddIn SelectedAddIn
        {
            get
            {
                return this.selectedAddIn;
            }
            set
            {
                this.selectedAddIn = value;
                RaisePropertyChanged(() => SelectedAddIn);
                RaisePropertyChanged(() => Detail);
                RaisePropertyChanged(() => Image);
            }
        }

        /// <summary>
        /// Get the Uri of the application image
        /// </summary>
        public ImageSource Image
        {
            get
            {
                if (selectedAddIn == null) return null;
                return selectedAddIn.Icon;
            }
        }

        /// <summary>
        /// Detail of the selected <see cref="selectedAddIn"/>
        /// </summary>
        public string Detail
        {
            get
            {
                if (selectedAddIn == null) return string.Empty;
                return string.Format("AddIn Namespace: {0}\r\nAddIn Extension: *.{1}\r\nAddIn Description: {2}", selectedAddIn.Namespace, selectedAddIn.Extension, selectedAddIn.Description);
            }
        }

        /// <summary>
        /// Version of the DSLPed desktop
        /// </summary>
        public string VersionString
        {
            get
            {
                return Assembly.GetExecutingAssembly().GetLongVersionString();
            }
        }

        #endregion
    }
}
